package com.zhike.b2b.role.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.zhike.b2b.Query;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.model.MenuVo;
import com.zhike.b2b.model.RoleVo;
import com.zhike.b2b.role.IRoleService;

@Service
public class RoleService extends BaseService implements IRoleService {
	
	private static final String CACHE_NAME = "menusCache";

	@Resource
	private CacheManager ehcacheManager;

	private Cache cache ;  
	
	/**
	 * 查询所有的角色
	 * @return
	 */
    public List<RoleVo> getAllRoles()
	{
		List<RoleVo> lu = jt.query(Query.role_GET_ROLE,
				 new RowMapper<RoleVo>() {
					public RoleVo mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						RoleVo rv = new RoleVo();
						rv.setRoleName(rs.getString("role_name"));
						rv.setRoleAuthId(rs.getString("role_auth_id"));
						return rv;
					}
				});
		return lu;
	}
    
	/**
	 * 根据用户的RoleId查询用户菜单列表
	 * @param roleId
	 * @return
	 */
    public List<MenuVo> getAllMenuInfoByAuthId(String roleId)
	{
		List<MenuVo> lu = jt.query(Query.role_GET_MENUINFOS_BY_ROLE_AUTH_ID,
				new Object[] { roleId }, new RowMapper<MenuVo>() {
					public MenuVo mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						MenuVo mv = new MenuVo();
						mv.setMenuId(rs.getString("menu_id"));
						mv.setMenuName(rs.getString("menu_name"));
						mv.setMenuPath(rs.getString("menu_path"));
						return mv;
					}
				});
		return lu;
	}
    
    public MenuVo getMenuByPath(String menuPath)
    {
    	 List<MenuVo> adminMenuList = getAllAdminMenus();
    	 for (int k = 0; k < adminMenuList.size(); k++)
         {
           MenuVo mv = (MenuVo)adminMenuList.get(k);
           String mp = mv.getMenuPath();
           if (mp.equals(menuPath))
                return mv;
         }
    	 return null;
    }
    /**
     * 根据菜单ID获取指定菜单对象
     * @param menuId
     * @return
     */
    public MenuVo getMenuBySubIdSix(String menuId)
    {
    	 List<MenuVo> adminMenuList = getAllAdminMenus();
    	 for (int k = 0; k < adminMenuList.size(); k++)
         {
           MenuVo mv = (MenuVo)adminMenuList.get(k);
           String mid = mv.getMenuId();
           if (mid.equals(menuId))
                return mv;
         }
    	 return null;
    }
    
    public List<MenuVo> getAdminMenuList(String level)
    {
      List<MenuVo> lm = new ArrayList<MenuVo>();
      List<MenuVo> adminMenuList = getAllAdminMenus();
     
      if (level.length() == 0)
      {
        for (int i = 0; i < adminMenuList.size(); i++)
        {
          MenuVo mv = (MenuVo)adminMenuList.get(i);
          if (mv.getMenuId().length() != 3)
            continue;
          lm.add(mv);
        }
      }else{
    	int i = level.length();
        int j = 0;
        if (i == 3)
          j = 6;
        else if (i == 6)
          j = 10;
        else if (i == 10)
          j = 15;
        else if (i == 15)
          j = 20;
        for (int k = 0; k < adminMenuList.size(); k++)
        {
          MenuVo mv = (MenuVo)adminMenuList.get(k);
          String menuId = mv.getMenuId();
          if ((menuId.length() != j) || (!menuId.substring(0, i).equals(level)))
             continue;
          lm.add(mv);
        }
      }
      return lm;
    }
    
    public List<MenuVo> getAllMenuInfo(String sysCode)
    {
      String sql = "select menu_id,menu_name,menu_path from menu_info where display='1' and subsys_code= ? order by sort_no,menu_id";
      List<MenuVo> lu = jt.query(sql,
				new Object[] { sysCode }, new RowMapper<MenuVo>() {
					public MenuVo mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						MenuVo mv = new MenuVo();
						mv.setMenuId(rs.getString("menu_id"));
						mv.setMenuName(rs.getString("menu_name"));
						mv.setMenuPath(rs.getString("menu_path"));
						return mv;
					}
				});
      return lu;
    }
    
    private List<MenuVo> getAllAdminMenus()
    {
    	cache = ehcacheManager.getCache(CACHE_NAME);  
  	  	Element element = cache.get("B2B.SYS");
  	  	List<MenuVo> adminMenuList;
        if (element == null)
        {
      	 logger.debug("读取系统管理后台菜单");
      	 adminMenuList = getAllMenuInfo("B2B.SYS");
      	 element = new Element("B2B.SYS",adminMenuList);
      	 cache.put(element);
        }else{
      	  adminMenuList =  (List<MenuVo>)element.getObjectValue();
        }
        return adminMenuList;
    }
}
